package com.basketball;

import com.basketball.entity.Player;
import com.basketball.entity.Record;
import com.basketball.entity.Skills;
import com.basketball.service.PlayerService;
import com.basketball.service.RecordService;
import com.basketball.service.SkillsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@SpringBootApplication
@EnableScheduling
@RestController
public class BasketBallApplication {
	public final static Logger logger = LoggerFactory.getLogger(BasketBallApplication.class);
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private String tdDate=dateFormat.format(new Date());

	@Autowired
	private PlayerService playerService;
	@Autowired
	private RecordService recordService;
	@Autowired
	private SkillsService skillsService;
	public static void main(String[] args) {
		SpringApplication.run(BasketBallApplication.class, args);
	}
	@RequestMapping("/test")
	public String test(){
		List<Skills> skillsList = skillsService.getAllSkills();
		String radomSkills ="投篮命中率+10%";
		//获取1-list.size中的随机数
		if(skillsList.size()>0){
			int i = (int)(1+Math.random()*(skillsList.size()-1+1));
			radomSkills = skillsList.get(i).getSname();
		}
		return "BasketBallClub version 0.0.9<br /> 您已报名成功<br /><br />【系统奖励随机技能："+radomSkills+"】<br /><br />请准时参加！ ";
	}

	/**
	 * 参加
	 * @return
	 */
	@RequestMapping("/imin/{sec}/{week_index}")
	public String imin(@PathVariable String sec,@PathVariable String week_index){
		Player player = playerService.getBySec(sec);
		if(player!=null){
			List<Skills> skillsList = skillsService.getAllSkills();
			Record record = new Record();
			record.setName(player.getName());
			logger.info(player.getName()+"imin tdDate"+week_index);
			record.setEventDate(week_index);
			record.setState("Y");
			recordService.addRecord(record);
			//更换seckey
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			logger.info("imin uuid"+uuid);
			player.setSec(uuid);
			playerService.save(player);
			String radomSkills ="投篮命中率+10%";
			//获取1-list.size中的随机数
			if(skillsList.size()>0){
				int i = (int)(1+Math.random()*(skillsList.size()-1+1));
				radomSkills = skillsList.get(i).getSname();
			}
			return "您已报名成功<br /><br />【系统奖励随机技能："+radomSkills+"】<br /><br />请准时参加！ ";
		}else{
			return "谁在用奇怪的方式报名？  ";
		}
	}

	/**
	 * 不参加
	 * @return
	 */
	@RequestMapping("/imout/{sec}")
	public String imout(@PathVariable String sec){
		Player player = playerService.getBySec(sec);
		if(player!=null){
//			Record record = new Record();
//			record.setName(player.getName());
//			record.setEventDate(dateFormat.format(new Date()));
//			record.setState("N");
//			recordService.addRecord(record);
			//更换seckey
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			player.setSec(uuid);
			logger.info("imout uuid"+uuid);
			playerService.save(player);
			return "期待下次,美好的相遇！ ";
		}else{
			return "谁在用奇怪的方式报名？  ";
		}
	}

	/**
	 * 退订
	 * @return
	 */
	@RequestMapping("/td/{sec}")
	public String td(@PathVariable String sec){
		Player player = playerService.getBySec(sec);
		if(player!=null){
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			player.setSec(uuid);
			player.setState("N");
			playerService.save(player);
			return "期待下次,美好的相遇！ ";
		}else{
			return "如果您已经点击了<参加>或者<下次一定>的按钮，则需要在下次收到邮件时退订！  ";
		}
	}
}
